﻿using System;
using System.Collections.Generic;
using System.Text;
using Watchdog.Bll.Base;
using Watchdog.Bll.Factory;

namespace Watchdog.Bll {

    public partial class Test : TestBase {

        internal Test () : base () {}

        internal Test ( Watchdog.Facade.Test test ) : base (test) { }
		
		#region [ Parent objects ]

		private Definition definition;
		public new Definition Definition {
			get {
				if ( definition == null )
					 definition = DefinitionFactory.GetById(base.Definition);
				return definition;
			}
			set { 
				definition = value;
				base.Definition = value.Id;
			}
		}

		#endregion

		#region [ Many to many relations ]

		List<Notification> notifications;
		public List<Notification> Notifications {
			get {
				if ( notifications == null )
					notifications = NotificationFactory.GetByTest(this.Id);
				return notifications;
			}
		}

		#endregion

		#region [ Add & Remove methods for many to many relations ]

        public void AddNotification ( Notification notification ) {
			TestFactory.AddNotification (this, notification);
			//reset the collection;
			notifications = null;
		}

        public void RemoveNotification ( Notification notification ) {
			TestFactory.RemoveNotification (this, notification);
			//reset the collection;
			notifications = null;
		}

		#endregion
		
		#region [ One to many relations ]

		List<Schedule> schedules;
		public List<Schedule> Schedules {
			get {
				if ( schedules == null )
					schedules = ScheduleFactory.GetByTest(this.Id);
				return schedules;
			}
		}

        //List<TestHistory> testhistorys;
        //public List<TestHistory> TestHistorys {
        //    get {
        //        if ( testhistorys == null )
        //            testhistorys = TestHistoryFactory.GetByTest(this.Id);
        //        return testhistorys;
        //    }
        //}

        //List<TestStatus> teststatuss;
        //public List<TestStatus> TestStatuss {
        //    get {
        //        if ( teststatuss == null )
        //            teststatuss = TestStatusFactory.GetByTest(this.Id);
        //        return teststatuss;
        //    }
        //}

		#endregion
		
		#region [ Add & Remove methods for one to many relations ]

		public void AddSchedule (Schedule schedule) {
			schedule.Test = this;
			schedule.Update();
			//TestFactory.AddSchedule (this, schedule);
			//reset the collection;
			schedules = null;
		}

		public void RemoveSchedule (Schedule schedule) {
			schedule.Test = this;
			schedule.Delete();
			//TestFactory.RemoveSchedule (this, schedule);
			//reset the collection;
			schedules = null;
		}

        //public void AddTestHistory (TestHistory testhistory) {
        //    testhistory.Test = this;
        //    testhistory.Update();
        //    //TestFactory.AddTestHistory (this, testhistory);
        //    //reset the collection;
        //    testhistorys = null;
        //}

        //public void RemoveTestHistory (TestHistory testhistory) {
        //    testhistory.Test = this;
        //    testhistory.Delete();
        //    //TestFactory.RemoveTestHistory (this, testhistory);
        //    //reset the collection;
        //    testhistorys = null;
        //}

        //public void AddTestStatus (TestStatus teststatus) {
        //    //teststatus.Test = this;
        //    teststatus.Update();
        //    //TestFactory.AddTestStatus (this, teststatus);
        //    //reset the collection;
        //    teststatuss = null;
        //}

        //public void RemoveTestStatus (TestStatus teststatus) {
        //    //teststatus.Test = this;
        //    teststatus.Delete();
        //    //TestFactory.RemoveTestStatus (this, teststatus);
        //    //reset the collection;
        //    teststatuss = null;
        //}

		#endregion

    }
}
